package com.weai.portal.service.impl;

import com.weai.portal.entity.AiPapers;
import com.weai.portal.repository.AiPapersRepository;
import com.weai.portal.service.AiPapersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * AI清单服务实现类
 */
@Service
@Slf4j
public class AiPapersServiceImpl implements AiPapersService {

    @Autowired
    private AiPapersRepository AiPapersRepository;

    @Override
    public List<AiPapers> getAllAiPapers() {
        log.debug("开始获取所有AI清单");
        try {
            List<AiPapers> aiPapers = AiPapersRepository.findAll();
            log.info("成功获取所有AI清单，数量: {}", aiPapers.size());
            return aiPapers;
        } catch (Exception e) {
            log.error("获取所有AI清单失败", e);
            throw e;
        }
    }

    @Override
    public List<AiPapers> searchByTitle(String title) {
        log.debug("开始搜索AI清单，标题关键词: {}", title);
        try {
            List<AiPapers> aiPapers = AiPapersRepository.searchByTitle(title);
            log.info("成功搜索AI清单，标题关键词: {}，数量: {}", title, aiPapers.size());
            return aiPapers;
        } catch (Exception e) {
            log.error("搜索AI清单失败，标题关键词: {}", title, e);
            throw e;
        }
    }

    @Override
    public AiPapers getAiPapersById(Long id) {
        log.debug("开始获取AI清单详情，ID: {}", id);
        try {
            AiPapers aiPapers = AiPapersRepository.findById(id)
                    .orElseThrow(() -> {
                        log.error("未找到AI清单，ID: {}", id);
                        return new RuntimeException("AiPapers not found with id: " + id);
                    });
            log.info("成功获取AI清单详情，ID: {}", id);
            return aiPapers;
        } catch (Exception e) {
            log.error("获取AI清单详情失败，ID: {}", id, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public AiPapers createAiPapers(AiPapers aiPapers) {
        log.debug("开始创建AI清单，标题: {}", aiPapers.getTitle());
        try {
            AiPapers savedAiPapers = AiPapersRepository.save(aiPapers);
            log.info("成功创建AI清单，ID: {}, 标题: {}", savedAiPapers.getId(), savedAiPapers.getTitle());
            return savedAiPapers;
        } catch (Exception e) {
            log.error("创建AI清单失败，标题: {}", aiPapers.getTitle(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public AiPapers updateAiPapers(Long id, AiPapers aiPapers) {
        log.debug("开始更新AI清单，ID: {}", id);
        try {
            AiPapers existingAiPapers = getAiPapersById(id);
            
            existingAiPapers.setTitle(aiPapers.getTitle());
            existingAiPapers.setDescript(aiPapers.getDescript());
            existingAiPapers.setUrlAddress(aiPapers.getUrlAddress());
            existingAiPapers.setUpdater(aiPapers.getUpdater());
            
            AiPapers updatedAiPapers = AiPapersRepository.save(existingAiPapers);
            log.info("成功更新AI清单，ID: {}", id);
            return updatedAiPapers;
        } catch (Exception e) {
            log.error("更新AI清单失败，ID: {}", id, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public void deleteAiPapers(Long id) {
        log.debug("开始删除AI清单，ID: {}", id);
        try {
            AiPapersRepository.deleteById(id);
            log.info("成功删除AI清单，ID: {}", id);
        } catch (Exception e) {
            log.error("删除AI清单失败，ID: {}", id, e);
            throw e;
        }
    }
} 